package com.spring.utils.api_v4.service.impl;

import com.spring.utils.api_v4.dao.*;
import com.spring.utils.api_v4.pojo.DeptInfo;
import com.spring.utils.api_v4.pojo.Password;
import com.spring.utils.api_v4.pojo.Role;
import com.spring.utils.api_v4.pojo.UserInfo;
import com.spring.utils.api_v4.pojo.response.BizException;
import com.spring.utils.api_v4.pojo.response.ResultCode;
import com.spring.utils.api_v4.pojo.salary.DeptWage;
import com.spring.utils.api_v4.pojo.salary.EmployeeWage;
import com.spring.utils.api_v4.service.ISystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

@Service
public class SystemService implements ISystemService {
    @Autowired private IRoleDao roleDao;
    @Autowired private IDeptDao deptDao;
    @Autowired private IUserDao userDao;
    @Autowired private IPwdDao pwdDao;
    @Autowired private IDeptWageDao deptWageDao;
    @Autowired private IEmployeeWageDao employeeWageDao;
    @Autowired private IEmployeeSalaryTaskDao employeeSalaryTaskDao;

    @Override
    public List<DeptInfo> getDeptsAll() {
        return deptDao.findAll();
    }

    @Override
    public void saveDept(DeptInfo dept) {
        // 根据名称查询部门
        DeptInfo dp = deptDao.findByName(dept.getName());
        // 如果该部门存在，且不是当前部门，则出错
        if(dp != null && dp.getId()!=dept.getId()){
            throw new BizException(ResultCode.DEPARTMENT_EXIST);
        }
        // 保存
        deptDao.save(dept);
    }

    @Override
    public DeptInfo getDeptById(int id) {
        Optional<DeptInfo> op = deptDao.findById(id);
        return op.isPresent() ? op.get() : null;
    }

    @Override
    public DeptInfo getDeptByName(String name) {
        return deptDao.findByName(name);
    }

    @Override
    public boolean deleteDeptById(int id) {
        Optional<DeptInfo> op = deptDao.findById(id);
        // 如果部门不存在
        if(op.isEmpty()) {
            throw new BizException(ResultCode.DEPARTMENT_NOT_EXIST);
        }
        deptDao.delete(op.get());
        return true;
    }

    @Override
    public DeptWage getDeptWage(DeptInfo dept) {
        return deptWageDao.findById(dept.getId());
    }

    @Override
    public DeptWage getDeptWage(int deptId) {
        return deptWageDao.findById(deptId);
    }

    @Override
    public List<DeptWage> getDeptWagesAll() {
        return deptWageDao.findAll();
    }

    @Override
    public void saveDeptWage(DeptWage deptWage) {
        deptWageDao.save(deptWage);
    }

    @Override
    public EmployeeWage getEmployeeWage(UserInfo user) {
        return employeeWageDao.findByUserInfo(user);
    }

    @Override
    public void saveEmployeeWage(EmployeeWage employeeWage) {
        employeeWageDao.save(employeeWage);
    }

    @Override
    public List<UserInfo> getStaffOfDept(DeptInfo dept) {
        return userDao.findAllByDepart(dept);
    }

    @Override
    public UserInfo findUserById(String id) {
        if(!StringUtils.hasText(id)){
            return null;
        }
        Optional<UserInfo> op = userDao.findById(id);
        return op.isPresent() ? op.get() : null;
    }

    @Override
    public List<Role> getUserRoles(UserInfo userInfo) {
        return roleDao.findByUserInfo(userInfo);
    }

    @Override
    public boolean hasRole(UserInfo user, String role) {
        return roleDao.existsByUserInfoAndName(user, role);
    }

    @Override
    public boolean hasRole(String userId, String role) {
        Optional<UserInfo> op = userDao.findById(userId);
        if(op.isEmpty()){
            throw new BizException(ResultCode.USER_NOT_EXIST);
        }
        return hasRole(op.get(), role);
    }

    @Override
    public Password getUserPwd(UserInfo user) {
        return pwdDao.findByUserInfo(user);
    }

    @Override
    public void savePwd(Password pwd) {
        pwdDao.save(pwd);
    }

    @Override
    public void saveUser(UserInfo user) {
        // 查询身份证和电话
        UserInfo u = userDao.findByIdCardOrPhone(user.getIdCard(), user.getPhone());
        // 如果存在且不是当前用户
        if(u != null && u.getId() != user.getId()) {
            throw new BizException(ResultCode.USER_EXIST);
        }
        userDao.save(user);
    }

    @Override
    public boolean userInfoExist(String idCard, String phone) {
        return userDao.findByIdCardOrPhone(idCard, phone) != null;
    }

    @Override
    public boolean userInfoExist(String id, String idCard, String phone) {
        UserInfo user = userDao.findByIdCardOrPhone(idCard, phone);
        // 如果用户存在，且编号不同
        return user != null && !user.getId().equals(id);
    }

    @Override
    @Transactional
    public void deleteUser(UserInfo user) {
        userDao.delete(user);
    }

    @Override
    public UserInfo getUserById(String userId) {
        Optional<UserInfo> op = userDao.findById(userId);
        return op.isPresent()? op.get(): null;
    }

    @Override
    public void resetUserPwd(UserInfo user) {
        Password pwd = pwdDao.findByUserInfo(user);
        // 如果该用户还没有设置密码
        if(pwd == null) {
            pwd = new Password();
            pwd.setUserInfo(user);
        }
        pwd.setPassword(user.getIdCard().substring(12));
        pwdDao.save(pwd);
    }

    @Override
    public List<Role> getUserRoles(String id) {
        return roleDao.findByUserInfoId(id);
    }

    @Override
    @Transactional
    public void deleteUserById(String userId) {
        Optional<UserInfo> op = userDao.findById(userId);
        if(op.isEmpty()) {
            throw new BizException(ResultCode.USER_NOT_EXIST);
        }
        UserInfo user = op.get();
        userDao.delete(user);
    }

    @Override
    public List<UserInfo> getUsersByDeptId(int deptId) {
        Optional<DeptInfo> op = deptDao.findById(deptId);
        if(op.isEmpty()) {
            throw new BizException(ResultCode.DEPARTMENT_NOT_EXIST);
        }
        return userDao.findAllByDepart(op.get());
    }
}
